package com.zhou.common.mybatis.template.shiro;

import lombok.extern.slf4j.Slf4j;
import org.flowable.cmmn.api.CmmnRuntimeService;
import org.flowable.cmmn.api.CmmnTaskService;
import org.flowable.cmmn.api.runtime.*;
import org.flowable.cmmn.engine.CmmnEngine;
import org.flowable.task.api.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;

import java.util.List;

@Slf4j
//@Component
public class CMMNTest3 implements CommandLineRunner {

    @Autowired
    private CmmnEngine  cmmnEngine;
    @Override
    public void run(String... args) throws Exception {
        CmmnRuntimeService cmmnRuntimeService = cmmnEngine.getCmmnRuntimeService();
        CmmnTaskService cmmnTaskService = cmmnEngine.getCmmnTaskService();

//        UserEventListenerInstanceQuery userEventListenerInstanceQuery = cmmnRuntimeService.createUserEventListenerInstanceQuery();

        CaseInstance caseInstance = cmmnRuntimeService.createCaseInstanceBuilder()
                .caseDefinitionKey("cmmn_test2")
                .start();
        List<PlanItemInstance> planItemInstances = cmmnRuntimeService.createPlanItemInstanceQuery()
                .caseInstanceId(caseInstance.getId())
                .orderByName().asc()
                .list();
        for (PlanItemInstance planItemInstance : planItemInstances) {
            System.out.println(planItemInstance.getName()
                    + ", state=" + planItemInstance.getState()
                    + ", parent stage=" + planItemInstance.getStageInstanceId());
        }

        UserEventListenerInstanceQuery userEventListenerInstanceQuery = cmmnRuntimeService.createUserEventListenerInstanceQuery();
        List<UserEventListenerInstance> list = userEventListenerInstanceQuery.list();
        for (UserEventListenerInstance userEventListenerInstance : list) {
            System.out.println(userEventListenerInstance.getName()
                    + ", state=" + userEventListenerInstance.getState()
                    + ", parent stage=" + userEventListenerInstance.getStageInstanceId());
            cmmnRuntimeService.completeUserEventListenerInstance(userEventListenerInstance.getId() );
        }
        List<GenericEventListenerInstance> list1 = cmmnRuntimeService.createGenericEventListenerInstanceQuery().list();
        for (GenericEventListenerInstance genericEventListenerInstance : list1) {
            System.out.println(genericEventListenerInstance.getName()
                    + ", state=" + genericEventListenerInstance.getState()
                    + ", parent stage=" + genericEventListenerInstance.getStageInstanceId());
            cmmnRuntimeService.completeGenericEventListenerInstance(genericEventListenerInstance.getId() );
        }

        planItemInstances = cmmnRuntimeService.createPlanItemInstanceQuery()
                .caseInstanceId(caseInstance.getId())
                .orderByName().asc()
                .list();
        for (PlanItemInstance planItemInstance : planItemInstances) {
            System.out.println(planItemInstance.getName()
                    + ", state=" + planItemInstance.getState()
                    + ", parent stage=" + planItemInstance.getStageInstanceId());
        }
        List<Task> employeeTasks = cmmnTaskService.createTaskQuery()
//                .caseInstanceId("9c461db9-c222-11ec-9f59-5414f3dd8146")
                .caseDefinitionKey("cmmn_test2")
                .taskAssignee("xianfeng2")
                .orderByTaskName().asc()
                .list();
        for (Task task : employeeTasks) {
            System.out.println("Task for employee: " + task);
            cmmnTaskService.complete(task.getId());
        }
    }
}
